﻿Public Class oCustomerGrade
    Public Function GetAllCustomerGrade(ByVal dataBaseName As String, ByVal companyCode As String, ByVal isAdmin As Boolean) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oCustomerGrade")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                If isAdmin Then
                    ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oCustomerGrade T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode JOIN oUser T2 ON T0.CreatedUserID = T2.UserID  ORDER BY T0.GradeID DESC")
                Else
                    ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oCustomerGrade T0 JOIN oCompany T1 ON T0.CompanyCode = T1.CompanyCode JOIN oUser T2 ON T0.CreatedUserID = T2.UserID  WHERE T0.CompanyCode = @Param1  ORDER BY T0.GradeID DESC", New Object() {companyCode})

                End If
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetCustomerGradeByCode(ByVal dataBaseName As String, ByVal gradeCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oCustomerGrade")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oCustomerGrade  T0 JOIN oCompany   T1 ON T0.CompanyCode = T1.CompanyCode  JOIN oUser T2 ON T0.CreatedUserID = T2.UserID WHERE GradeCode = @Param1", New Object() {gradeCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetCustomerGradeByID(ByVal dataBaseName As String, ByVal gradeID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oCustomerGrade")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oCustomerGrade  T0 JOIN oCompany   T1 ON T0.CompanyCode = T1.CompanyCode  JOIN oUser T2 ON T0.CreatedUserID = T2.UserID WHERE GradeID = @Param1", New Object() {gradeID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function UpdateJsonCustomerGrade(ByVal gradeID As String, _
                                          ByVal gradeCode As String, ByVal gradeName As String, ByVal companyCode As String, _
                                          ByVal createdUserId As String, ByVal minValue As Double, ByVal pointFactor As Double, _
                                          ByVal pointCurrency As Double, _
                                          ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {companyCode, gradeCode, gradeName, minValue, pointFactor, pointCurrency, createdUserId}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oCustomerGrade]")
                    query.Append("  ([CompanyCode]")
                    query.Append(" ,[GradeCode]")
                    query.Append(" ,[GradeName]")
                    query.Append(" ,[MinValue]")
                    query.Append(" ,[PointFactor]")
                    query.Append(" ,[PointCurrency]")
                    query.Append(" ,[CreatedUserID])")
                    query.Append("VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6,@Param7)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oCustomerGrade] SET ")
                    query.Append(" [CompanyCode] = @Param1")
                    query.Append("  ,[GradeCode] = @Param2")
                    query.Append("  ,[GradeName] = @Param3")
                    query.Append("  ,[MinValue] = @Param4")
                    query.Append("  ,[PointFactor] = @Param5")
                    query.Append("  ,[PointCurrency] = @Param6")
                    query.Append(" WHERE [GradeID] = @Param7")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {companyCode, gradeCode, gradeName, minValue, pointFactor, pointCurrency, gradeID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonCustomerGrade(ByVal dataBaseName As String, ByVal gradeID As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {gradeID}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oCustomerGrade]")
                query.Append("  WHERE GradeID =  @Param1")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
End Class
